home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / Dots & Pixels / headers / stopwatch.h < prev    next >
C/C++ Source or Header  |  1995-09-29  |  1KB  |  68 lines

  1. #pragma once
  2. //
  3. // stopwatch, a class to measure intervals up to about 35 minutes in length with
  4. // a maximal resolution of 20 microseconds (see IM VI, The Time Manager for details)
  5. //
  6. // 'read' and 'stop' return the number of microseconds since 'start' or zero
  7. // if the elapsed time is too great or 'start' wasn't called at all.
  8. // The utility function 'seconds' converts those times into seconds.
  9. //
  10. class stopwatch : public TMTask
  11. {
  12.     public:
  13.         stopwatch();
  14.         ~stopwatch();
  15.         void start();
  16.         long read();
  17.         double seconds();
  18.         double milliseconds();
  19.         double microseconds();
  20.  
  21.         long stop();
  22.         
  23.         static double seconds( long stopwatch_result);
  24.         static double milliseconds( long stopwatch_result);
  25.         static double microseconds( long stopwatch_result);
  26.         
  27.     private:
  28.         static const long starting_value;
  29.         int    running;
  30.         
  31.         void restart( unsigned long to_go);        
  32. };
  33.  
  34. inline void stopwatch::start()
  35. {
  36.     restart( starting_value);
  37. }
  38.  
  39. inline double stopwatch::seconds()
  40. {
  41.     return ((double) read()) / 1000000.0;
  42. }
  43.  
  44. inline double stopwatch::milliseconds()
  45. {
  46.     return ((double) read()) / 1000.0;
  47. }
  48.  
  49. inline double stopwatch::microseconds()
  50. {
  51.     return (double) read();
  52. }
  53.  
  54. inline double stopwatch::seconds( long stopwatch_result)
  55. {
  56.     return ((double) stopwatch_result) / 1000000.0;
  57. }
  58.  
  59. inline double stopwatch::milliseconds( long stopwatch_result)
  60. {
  61.     return ((double) stopwatch_result) / 1000.0;
  62. }
  63.  
  64. inline double stopwatch::microseconds( long stopwatch_result)
  65. {
  66.     return (double) stopwatch_result;
  67. }
  68.